package com.h2cloud.JUCTest.reentrantLock;

import java.util.concurrent.locks.ReentrantLock;

public class TestFair {
	public static void main(String[] args) throws InterruptedException {
		ReentrantLock lock = new ReentrantLock(false);

		lock.lock();
		for (int i = 0; i < 200; i++) {
			new Thread(() -> {
				lock.lock();
				try {
					System.out.println(Thread.currentThread().getName() + " running...");
				} finally {
					lock.unlock();
				}
			}, "t" + i).start();
		}

		// 1s 之后去争抢锁
		Thread.sleep(1000);
		new Thread(() -> {
			lock.lock();
			try {
				System.out.println(Thread.currentThread().getName() + " running...");
			} finally {
				lock.unlock();
			}
		}, "强行插入 ").start();

		lock.unlock();
	}
}
